<?

global $_APP_SESSION_THEME;

require_once ("class.indexing.inc");
require_once ("{$_APP_SESSION_THEME}/template.list.inc");

function _app_get_indexes ($indexes) {

	$r_indexes = array ();
	$_indexes = array ();

	foreach ($indexes as $key => $val) {
		
		if (isset ($val["access"]) &&
				!_app_verify_object_data_right ($val["access"]))
			continue;
		
		$_indexes[$key] = $val["order"];
		$r_indexes[$key] = $val;
	}
	
	return array ($r_indexes, $_indexes);
}

function _app_set_filter ($list_id, $indexes, $list_count, $list_select, $list_check_box) {

	/* there is an issue with the pattern matching on the second match giving
	   a Compilation failed: nothing to repeat at offset 25
	   message when checkin on GROUP BY and ORDER BY matches.
	   Turning off error_reporting for now will not show the warning messages
	   then turn them back on for further reporting at the end of this
	   function call. */
	$error_level = error_reporting (0);

	$_OBJECT_LIST_FILTER = new Container ();
	$_OBJECT_LIST_FILTER->set_callback_begin ("_template_list_filter");
	$_OBJECT_LIST_FILTER->set_option ("callback_end", false);
	$_OBJECT_LIST_FILTER->set_variable ("indexes", $indexes);
	$_OBJECT_LIST_FILTER->set_variable ("list_check_box", true);
	$_OBJECT_LIST_FILTER->set_variable ("list_id", $list_id);
	$_OBJECT_LIST_FILTER->set_variable ("current_path", $_SERVER["QUERY_STRING"]);

	if (!isset ($_REQUEST["filter_{$list_id}"])) {

		if (isset ($_SESSION["filter_{$list_id}"])) {

			$_REQUEST["filter_{$list_id}"] = $_SESSION["filter_{$list_id}"];
		}
	} else {

		$_SESSION["filter_{$list_id}"] = $_REQUEST["filter_{$list_id}"];
	}

	if (!isset ($_REQUEST["filter_{$list_id}_value"])) {

		if (isset ($_SESSION["filter_{$list_id}_value"])) {

			$_REQUEST["filter_{$list_id}_value"] = $_SESSION["filter_{$list_id}_value"];
		}
	} else {

		$_SESSION["filter_{$list_id}_value"] = $_REQUEST["filter_{$list_id}_value"];
	}

	if (isset ($_REQUEST["filter_{$list_id}_reset"])) {

		$_REQUEST["filter_{$list_id}_value"] = null;
		$_REQUEST["filter_{$list_id}"] = null;
		$_SESSION["filter_{$list_id}_value"] = null;
		$_SESSION["filter_{$list_id}"] = null;
	}

	if ($filter = _app_get_filter ("filter_{$list_id}", $indexes)) {

		if (preg_match ("/^(SELECT)(.*)((GROUP BY)*.*((GROUP BY){1}).*)$/ims", $list_count, $a)) {

			$list_count = $a[1].$a[2];
			$count_query = " ".$a[3];
		} elseif (preg_match ("/^(SELECT)(.*)((GROUP BY)+*((GROUP BY){0,1}).*)$/ims", $list_count, $a)) {

			$list_count = $a[1].$a[2];
			$count_query = " ".$a[3];
		} elseif (preg_match ("/^(SELECT)(.*)((ORDER BY)*.*((ORDER BY){1}).*)$/ims", $list_count, $a)) {

			$list_count = $a[1].$a[2];
			$count_query = " ".$a[3];
		} elseif (preg_match ("/^(SELECT)(.*)((ORDER BY)+*((ORDER BY){0,1}).*)$/ims", $list_count, $a)) {

			$list_count = $a[1].$a[2];
			$count_query = " ".$a[3];
		}

		if (preg_match ("/(FROM{1}(.*WHERE.*)WHERE){1}/ims", $list_count)) $list_count .= " WHERE ({$filter["filter_count"]})";
		elseif (!preg_match ("/WHERE/ims", $list_count)) $list_count .= " WHERE ({$filter["filter_count"]})";
		else  $list_count .= " AND ({$filter["filter_count"]})";

		if (preg_match ("/^(SELECT)(.*)((GROUP BY)*.*((GROUP BY){1}).*)$/ims", $list_select, $a)) {

			$list_select = $a[1].$a[2];
			$select_query = " ".$a[3];
		} elseif (preg_match ("/^(SELECT)(.*)((GROUP BY)+*((GROUP BY){0,1}).*)$/ims", $list_select, $a)) {

			$list_select = $a[1].$a[2];
			$select_query = " ".$a[3];
		} elseif (preg_match ("/^(SELECT)(.*)((ORDER BY)*.*((ORDER BY){1}).*)$/ims", $list_select, $a)) {

			$list_select = $a[1].$a[2];
			$select_query = " ".$a[3];
		} elseif (preg_match ("/^(SELECT)(.*)((ORDER BY)+*((ORDER BY){0,1}).*)$/ims", $list_select, $a)) {

			$list_select = $a[1].$a[2];
			$select_query = " ".$a[3];
		}

		if (preg_match ("/(FROM{1}(.*WHERE.*)WHERE){1}/ims", $list_select)) $list_select .= " WHERE ({$filter["filter"]})";
		elseif (!preg_match ("/WHERE/ims", $list_select)) $list_select .= " WHERE ({$filter["filter"]})";
		else  $list_select .= " AND ({$filter["filter"]})";

		$list_count .= $count_query;
		$list_select .= $select_query;
	}

	error_reporting ($error_level);

	#print $list_select."<br>";
	#print $list_count."<br>";
	return array ($_OBJECT_LIST_FILTER, $list_count, $list_select);
}

function _app_get_filter ($filter, $indexes) {

	$_filter = array ();

	if (!count ($_REQUEST[$filter])) return null;

	foreach ($_REQUEST[$filter] as $key => $val) {

		$value = preg_replace ("/ /", "%", $_REQUEST[$filter."_value"][$key]);

		$val = isset ($indexes[$val]["alias"]) ? $indexes[$val]["alias"] : $val;
		$_filter[] = "{$val} LIKE '%{$value}%'";
	}

	if (count ($_filter)) {

		return array (filter=>$filter=join (" OR ", $_filter), filter_count=>$filter);
	} else return null;
}

function _app_create_list_object ($options) {

	$db_conn = $options["db_conn"];
	$indexes = $options["indexes"];
	$list_count = $options["list_count"];
	$list_count_rows = isset ($options["list_count_rows"]) ? $options["list_count_rows"] : false;
	$list_select = $options["list_select"];
	$list_id = $options["list_id"];
	$list_check_box = $options["list_check_box"];
	$list_title = $options["list_title"];
	$list_options = isset ($options["list_options"]) ? $options["list_options"] : array ();
	$list_access = isset ($options["list_access"]) ? $options["list_access"] : array ();
	$list_limit = isset ($options["list_limit"]) ? $options["list_limit"] : 5;
	$primary_key = $options["primary_key"];
	$primary_relation = $options["primary_relation"];
	$index_keys = $options["index_keys"];
	$link_options = $options["link_options"];
	#$object_ctl = $options["object_ctl"];
	$object_ctl = isset ($options["object_ctl"]) ? $options["object_ctl"] : null;
	$object_access = $options["object_access"];
	$no_scrolling = $options["no_scrolling"];
	$no_indexing = $options["no_indexing"];

	if (preg_match ("/^(SELECT)(.*)((FROM)*.*((FROM){1}).*)$/sim", $list_select, $a)) {
	
		$list_count = preg_replace ("/^(SELECT)(.*)((FROM)*.*((FROM){1}).*)$/sim", "$1 COUNT(*) $3",$list_select);
	} elseif (preg_match ("/^(SELECT)(.*)((FROM)+.*((FROM){0,1}).*)$/sim", $list_select, $a)) {

		$list_count = preg_replace ("/^(SELECT)(.*)((FROM)+.*((FROM){0,1}).*)$/sim", "$1 COUNT(*) $3",$list_select);
	}

	if ($list_access && !_app_verify_object_data_right ($list_access))

		return null;
	
	list ($_OBJECT_LIST_FILTER, $list_count, $list_select) =
			_app_set_filter ($list_id, $indexes, $list_count, $list_select, $list_check_box);

	$_list_control = new Indexing ($db_conn, $list_count, $list_count_rows);

	//if ($_list_control->count == "0") return null;

	$_OBJECT_IFACE_LIST = new Container ();
	$_OBJECT_IFACE_LIST->set_callback_begin ("_template_list_begin");
	$_OBJECT_IFACE_LIST->set_callback_end ("_template_list_end");
	$_OBJECT_IFACE_LIST->set_variable ("list_id", $list_id);
	$_OBJECT_IFACE_LIST->set_variable ("list_title", $list_title);
	$_OBJECT_IFACE_LIST->set_variable ("list_check_box", $list_check_box);

	list ($indexes, $_indexes) = _app_get_indexes ($indexes);

	$_OBJECT_IFACE_LIST->insert_container ($_OBJECT_LIST_FILTER);

	$_OBJECT_IFACE_LIST->set_variable ("column_count", count ($indexes) + ($list_check_box ? 1 : 0));
	
	if (! isset ($no_indexing)) {
		
		/* $list_order = isset ($_REQUEST[$list_id . "_order"]) ?
				$_REQUEST[$list_id . "_order"] :
				null;
		$list_page = isset ($_REQUEST[$list_id . "_page"]) ?
				$_REQUEST[$list_id . "_page"] :
				null;
		$list_limit = isset ($_REQUEST[$list_id . "_limit"]) ?
				$_REQUEST[$list_id . "_limit"] :
				null;
		$list_scroll = isset ($_REQUEST[$list_id . "_scroll"]) ?
				$_REQUEST[$list_id . "_scroll"] :
				null;
		$list_action = isset ($_REQUEST[$list_id . "_action"]) ?
				$_REQUEST[$list_id . "_action"] :
				null; */
		$list_order = isset ($_REQUEST[$list_id . "_order"]) ?
				$_REQUEST[$list_id . "_order"] :
				$_SESSION[$list_id . "_order"];
		$list_page = isset ($_REQUEST[$list_id . "_page"]) ?
				$_REQUEST[$list_id . "_page"] :
				$_SESSION[$list_id . "_page"];
		$list_limit = isset ($_REQUEST[$list_id . "_limit"]) ?
				$_REQUEST[$list_id . "_limit"] :
				isset ($_SESSION[$list_id . "_limit"]) ?
				$_SESSION[$list_id . "_limit"] :
				$list_limit;
		$list_scroll = isset ($_REQUEST[$list_id . "_scroll"]) ?
				$_REQUEST[$list_id . "_scroll"] :
				$_SESSION[$list_id . "_scroll"];
		$list_action = isset ($_REQUEST[$list_id . "_action"]) ?
				$_REQUEST[$list_id . "_action"] :
				$_SESSION[$list_id . "_action"];
		if (!$list_page) $list_action = "first";
		//if ($list_page >  ($_list_control->count / $list_limit)) $list_page = 1;
		$_list_control->limit = $list_limit;

		foreach (array ("_order", "_page", "_limit", "_scroll", "_action")
				as $option) {

			$variable = "list" . $option;
			$_SESSION[$list_id . $option] = $$variable;
		}

		$_list_control->order ($list_order, $_indexes);
		$_list_control->scroll ($list_page, $list_limit, $list_action);

		$_OBJECT_LIST_INDEXES = new Container ();
		$_OBJECT_LIST_INDEXES->set_callback_begin
				("_template_list_indexes_begin");
		$_OBJECT_LIST_INDEXES->set_callback_end
				("_template_list_indexes_end");
		$_OBJECT_LIST_INDEXES->set_variable ("list_id", $list_id);

		if ($list_check_box) {
			$indexes = array_merge (array (checkbox=>array (label=>"",order=>"NONE", no_index=>true, callback=>"_template_list_row_checkbox")), $indexes);

		}

		foreach ($indexes as $key => $val) {

			$_OBJECT_LIST_INDEX = new Container ();
			$_OBJECT_LIST_INDEX->set_callback_begin
					("_template_list_index");
			$_OBJECT_LIST_INDEX->set_option
					("callback_end", false);
			$_OBJECT_LIST_INDEX->set_variable
					("list_id", $list_id);
			$_OBJECT_LIST_INDEX->set_variable
					("label", $val["label"]);
			$_OBJECT_LIST_INDEX->set_variable
					("options", $val);
			$_OBJECT_LIST_INDEX->set_variable
					("link_options", $link_options);
			$_OBJECT_LIST_INDEX->set_variable ("class_a",
					$_list_control->current == $key
					? "vmc_order_c" : "vmc_order_b");
			$_OBJECT_LIST_INDEX->set_variable ("class_b",
					$_list_control->current == $key
					? "vmc_order_a" : "vmc_order_a");
			$_OBJECT_LIST_INDEX->set_variable ("list_id", $list_id);
			$_OBJECT_LIST_INDEX->set_variable ("order",
				$key . "^" . $_list_control->order);
			$_OBJECT_LIST_INDEX->set_variable ("image",
					#$_list_control->cols[$key] . ".gif");
					"vmc_index_order_" .
					strtolower ($_list_control->cols[$key]));

			$_OBJECT_LIST_INDEXES->insert_container ($_OBJECT_LIST_INDEX);
		}

		$_OBJECT_IFACE_LIST->insert_container ($_OBJECT_LIST_INDEXES);
	
	}
	
	$_list_control->get_list ($list_select);
	$row_count = 0;

	/* begin new no_scrolling mechanism */
	if ($_list_control->rows && ((!$db_conn->num_rows ($_list_control->rows)) ||
			($db_conn->num_rows ($_list_control->rows) < $_list_control->limit)))

		$no_scrolling = true;
	else $no_scrolling = true;
	if ($_list_control->count > $_list_control->limit) $no_scrolling = null;
	/* end new no_scrolling mechanism */

	while ($row = $db_conn->fetch_object ($_list_control->rows)) {

		$_OBJECT_LIST_ROW = new Container ();
		$_OBJECT_LIST_ROW->set_callback_begin
				("_template_list_row_begin");
		$_OBJECT_LIST_ROW->set_callback_end
				("_template_list_row_end");

		$_OBJECT_LIST_ROW->set_option ("class",
				($row_count++ % 2 ? "vmc_list_control_row_c" :
			                 "vmc_list_control_row_a"));
		$_OBJECT_LIST_ROW->set_variable ("indexes", $indexes);

		/* if ($list_check_box) {

			$_OBJECT_ROW_COLUMN = new Container ();
			$_OBJECT_ROW_COLUMN->set_option ("callback_end", false);
			$_OBJECT_ROW_COLUMN->set_callback_begin (
					"_template_list_row_checkbox");
			$_OBJECT_ROW_COLUMN->set_variable ("list_id", $list_id);
			$_OBJECT_ROW_COLUMN->set_variable ("value", $row->$primary_key);
			$_OBJECT_ROW_COLUMN->set_variable ("row", $row);
			
			$_OBJECT_LIST_ROW->insert_container
					($_OBJECT_ROW_COLUMN);

		} */

		foreach ($indexes as $key => $val) {

			$value = isset ($val["values"]) ? ($sval = $val["values"][$row->$key]) ? $sval : $row->$key : $row->$key;
			$_OBJECT_ROW_COLUMN = new Container ();
			$_OBJECT_ROW_COLUMN->set_option ("callback_end", false);
			$_OBJECT_ROW_COLUMN->set_callback_begin (
					"_template_list_row_column");
			$_OBJECT_ROW_COLUMN->set_variable ("column", $val);
			$_OBJECT_ROW_COLUMN->set_variable ("value", $value);
			$_OBJECT_ROW_COLUMN->set_variable ("row", $row);
			$_OBJECT_ROW_COLUMN->set_variable ("list_id", $list_id);
			
			$_OBJECT_LIST_ROW->insert_container
					($_OBJECT_ROW_COLUMN);
					
			if ((isset ($object_access) &&
					$access = _app_verify_object_data_right ($object_access))
					|| !isset ($object_access)) {

				if (isset ($index_keys) and is_array ($index_keys)) {

					$ikeys = null;
					foreach ($index_keys as $index) {

						$ikeys .= "&{$index}={$row->$index}";
					}
				}

				$_OBJECT_ROW_COLUMN->set_variable ("object_ctl",
					$object_ctl ? 
					$object_ctl . ($primary_relation ?
					$primary_relation : $primary_key) . "=" .
					$row->$primary_key . $ikeys : null);
			} else $_OBJECT_ROW_COLUMN->set_variable ("object_ctl_off", true);
				
		}

		$_OBJECT_IFACE_LIST->insert_container ($_OBJECT_LIST_ROW);
	}

	$_OBJECT_LIST_OPTIONS = new Container ();
	$_OBJECT_LIST_OPTIONS->set_callback_begin
			("_template_list_options_begin");
	$_OBJECT_LIST_OPTIONS->set_callback_end
			("_template_list_options_end");
	$_OBJECT_LIST_OPTIONS->set_variable ("object_id", $list_id);
	$_OBJECT_IFACE_LIST->insert_container ($_OBJECT_LIST_OPTIONS);
		
	if (! isset ($no_scrolling)) {
		
		$_OBJECT_LIST_OPTIONS->set_variable
				($list_id . "_limit", $_list_control->limit);
		$_OBJECT_LIST_OPTIONS->set_variable
				($list_id . "_page", $_list_control->page);
		$_OBJECT_LIST_OPTIONS->set_variable
				($list_id . "_pages", $_list_control->pages);
		$_OBJECT_LIST_OPTIONS->set_variable
				($list_id . "_next", $_list_control->next);
		$_OBJECT_LIST_OPTIONS->set_variable
				($list_id . "_prev", $_list_control->prev);
		$_OBJECT_LIST_OPTIONS->set_variable ("link_options", $link_options);

	}

	foreach ($list_options as $option) {
		
		if (isset ($option["access"]) &&
				!_app_verify_object_data_right ($option["access"]))
		
			continue;

		$_OBJECT_LIST_OPTION = new Container ();

		if (isset ($option["javascript"]))

			$_OBJECT_LIST_OPTION->set_callback_begin ("_template_list_js_option");
		else $_OBJECT_LIST_OPTION->set_callback_begin ("_template_list_option");

		$_OBJECT_LIST_OPTION->set_callback_begin ("_template_list_option");
		$_OBJECT_LIST_OPTION->set_option ("callback_end", false);

		foreach ($option as $key => $val) {

			$_OBJECT_LIST_OPTION->set_option ($key, $val);
		}

		$_OBJECT_LIST_OPTIONS->insert_container ($_OBJECT_LIST_OPTION);
	}

	return $_OBJECT_IFACE_LIST;
}

?>
